<template>
  <div class="add">
    <DetailsFrom @addFn="addFn">
      <template>
        <div>
          <el-form ref="addForm" :model="addForm" label-position="top" :rules="addFormRules">
            <el-row>
              <el-col :span="6">
                <el-form-item label="库区编码" prop="code">
                  <el-input v-model="addForm.code" :disabled="true" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="所属仓库" prop="warehouseId">
                  <el-select v-model="addForm.warehouseId" placeholder="请选择" clearable>
                    <el-option v-for="(item) in wareList" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="库区名称" prop="name">
                  <el-input v-model="addForm.name" placeholder="请输入" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="温度类型" prop="temperatureType">
                  <el-select v-model="addForm.temperatureType" placeholder="请选择" clearable>
                    <el-option label="常温" value="CW" />
                    <el-option label="冷藏" value="LC" />
                    <el-option label="恒温" value="HW" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="承重类型" prop="bearingType">
                  <el-select v-model="addForm.bearingType" placeholder="请选择" clearable>
                    <el-option label="重型" value="ZX" />
                    <el-option label="中型" value="BX" />
                    <el-option label="轻型" value="QX" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="用途属性" prop="useType">
                  <el-select v-model="addForm.useType" placeholder="请选择" clearable>
                    <el-option label="入库缓存区" value="RKHCQ " />
                    <el-option label="出库缓存区" value="CKHCQ " />
                    <el-option label="存储区" value="CCQ " />
                    <el-option label="分拣区" value="FJQ " />
                    <el-option label="质检区" value="ZJQ " />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="负责人" prop="personName">
                  <el-input v-model="addForm.personName" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="联系电话" prop="phone">
                  <el-input v-model="addForm.phone" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="仓库状态" prop="status">
                  <el-radio v-model="addForm.status" :label="0">启用</el-radio>
                  <el-radio v-model="addForm.status" :label="1">停用</el-radio>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </template>
    </DetailsFrom>
  </div>
</template>

<script>
import { warehouseList, nextFn, searchArea, addArea, editArea } from '@/api'
import DetailsFrom from '@/components/DetailsFrom'
import { messageFn } from '@/utils/myutils'
export default {
  components: { DetailsFrom },
  data() {
    return {
      addForm: {},
      addFormRules: {
        warehouseId: [{ required: true, message: '请选择仓库类型' }],
        name: [{ required: true, message: '请输入库区名称' }],
        temperatureType: [{ required: true, message: '请选择温度类型' }],
        bearingType: [{ required: true, message: '请选择承类型' }],
        useType: [{ required: true, message: '请选择用途类型' }],
        personName: [{ required: true, message: '请输入负责人' }],
        phone: [{ pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, trigger: 'blur', message: '请输入正确手机号' }]
      },
      wareList: []
    }
  },
  // 组件路由首位 修改meta的名字
  beforeRouteEnter(to, from, next) {
    if (to.params.id === 'null') {
      to.meta.title = '新增库区'
    } else {
      to.meta.title = '修改库区'
    }
    next()
  },
  async created() {
    const res = await warehouseList({ status: 1 })
    this.wareList = res.data.data
    console.log(this.wareList)
    if (this.$route.params.id === 'null') {
      const res = await nextFn('KQ')
      this.$set(this.addForm, 'code', res.data.data)
      this.$set(this.addForm, 'status', 0)
      this.$forceUpdate()
    } else {
      const res = await searchArea(this.$route.params.id)
      this.addForm = res.data.data
    }
  },
  methods: {
    addFn() {
      console.log(this.$route.meta.title)
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) return
        let res
        if (this.addForm.id) {
          res = await editArea(this.addForm)
        } else {
          res = await addArea(this.addForm)
        }
        messageFn(res.data.isSuccess, this.$route.meta.title)
        this.$router.push({ name: 'area' })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/my-form.scss';
</style>
